/**
 * 
 */

/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  poolint.integration;




import java.sql.ResultSet;
import java.util.*;
import java.sql.ResultSetMetaData;
import java.util.TimerTask;

import poolint.pulldata.Operation;
import utility.CapitalChar;
import dbmanager.DBManager;
import dbmanager.DBOperation;
import dbmanager.GetIdName;


/**
 * @author Divyesh
 * */
public class PoolTable extends TimerTask
{
	private String POOLTABLE = "pooltable";
	
	DBOperation IntegrateDB;
	
	GetIdName gin;
	
	private String integrationObject;
	//private String reflactSchemaProperty = "property_details";
	//private String reflactSchemaMaster = "master_table";

	public PoolTable() {
		gin = new GetIdName();
		POOLTABLE = gin.getId(POOLTABLE);
	}
	
	public void setConnectionObject(String object)
	{
		Poolconnection poolConnect = new Poolconnection();
		poolConnect.setPoolconnection(object);
		IntegrateDB = poolConnect.getConnect();
	}
	public void setIntegrationObject(String integrationObject)
	{
		this.integrationObject = integrationObject;
	}
	
	/**
	 * readPoolTable() method will read the pooltable from the product database(Sugar, OB, etc.) and take a record set from respected table(s).
	 * After that it will stores the respected values of from the table into the vector through Hashtable.
	 * */
	@SuppressWarnings("unchecked")
	public void readPoolTable() 
	{
		try 
		{
			String poolTableId = "0";
			ResultSet rs = IntegrateDB.getSelect("SELECT * FROM pooltable where updated = FALSE order by id");
			
			while(rs.next())
			{
				poolTableId = rs.getString("id");
				String tableName = rs.getString("tableName");
				String process = rs.getString("process");
				
				ResultSet rs2 = IntegrateDB.getSelect( "SELECT * FROM  poolcolumn WHERE id = " + poolTableId );
				
				//ResultSetMetaData rs2mtdt = rs2.getMetaData();
				//int colCount = rs2mtdt.getColumnCount();
				//Hashtable  columnName = new Hashtable(colCount);
				
				Hashtable< Object, Object > columnNameValue = new Hashtable< Object, Object > ();
				Hashtable< Object, Object > oldColumnNameValue = new Hashtable< Object, Object > (); 
							
				while( rs2.next() )
				{
					String colName = rs2.getString( "columnname" );
					String colValue = rs2.getString( "columnvalue" );
					String oldColValue = null;
					try {
						oldColValue = rs2.getString( "oldcolumnvalue" );
						columnNameValue.put( colName, colValue );
						oldColumnNameValue.put( colName, oldColValue );
					} catch (Exception e) {}
				}
				rs2.close();
			/*					
				//String [] columnName = new String [colCount];
				//String columnName = rs2.getString("columnName");
				//String newcolumnNameValue = rs2.getString("columnValue");
				//String oldcolumnNameValue = rs2.getString("oldColumnValue");
			*/
				
				String table = getPoolTablename(tableName);	
					
				Operation operation;
				try 
				{										
					Class C = Class.forName("poolint.pulldata."+CapitalChar.makeFirstCharCapital(process));
					operation = (Operation) C.newInstance();
					
					operation.setColumnNameValue(columnNameValue);
					operation.setIntegratDatabase(IntegrateDB);
					operation.setIntegrationObject(integrationObject);
					//operation.setNewColumnValue(newcolumnNameValue);
					operation.setOldColumnNameValue(oldColumnNameValue);
					operation.setProcess(process);
					operation.setTableName(tableName);
					operation.setPoolTableName(table);
						
					if(operation.performOperation())
					{
						System.out.println(process + " done SUCCESSFULLY with Integration... For table : " + tableName);
						IntegrateDB.getUpdate("UPDATE pooltable SET updated = TRUE WHERE id = " + poolTableId);
					}
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
					IntegrateDB.getUpdate("UPDATE pooltable SET updated = TRUE WHERE id = " + poolTableId);
				}
			//}		
			//rs2.close();
			}		
			rs.close();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}		
	}
	
	
	private String getPoolTablename(String tableName) 
	{
		String table;
		try
		{
			String strTablename = gin.getId("tablename");
			String strIntegrateTablename = gin.getId(tableName);
			
			String query = "SELECT q.pv as tablename FROM property_details p JOIN property_details q ON" ;					
			query = query + "(p.mid = q.mid) WHERE p.pid = '"+strTablename+"' AND p.pv = '"+strIntegrateTablename+"' AND q.pid = '"+POOLTABLE+"'";
			
			ResultSet rs1 = DBManager.getSelect(query);
			rs1.next();
			table = rs1.getString("tablename");	
			table = gin.getItem(table);
			rs1.close();
		} 
		catch (Exception e) {
			table = "Master_table";
		}
		return table;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		PoolTable pt = new PoolTable();
		pt.setIntegrationObject("8265");
		pt.setConnectionObject("8273");
		
		pt.readPoolTable();
	}

	@Override
	public void run() 
	{
		readPoolTable();
	}
}
